package org.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.reggie.common.R;
import org.example.reggie.dto.DishDto;
import org.example.reggie.entity.Category;
import org.example.reggie.entity.Dish;
import org.example.reggie.entity.DishFlavor;
import org.example.reggie.service.CategoryService;
import org.example.reggie.service.DishFlavorService;
import org.example.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
//        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDto> dtoInfo = new Page<>();
        LambdaQueryWrapper<Dish> lq = new LambdaQueryWrapper<>();
        lq.like(name != null, Dish::getName, name);
        lq.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo, lq);

        BeanUtils.copyProperties(pageInfo, dtoInfo, "records");
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list = records.stream().map(item -> {
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(item, dto);
            Long id = item.getCategoryId();
            Category category = categoryService.getById(id);
            if (category != null) {
                String cname = category.getName();
                dto.setCategoryName(cname);
            }
            return dto;
        }).collect(Collectors.toList());
        dtoInfo.setRecords(list);

        return R.success(dtoInfo);
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable long id) {
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
//        log.info(dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

/*    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish) {
        LambdaQueryWrapper<Dish> lq = new LambdaQueryWrapper<>();
        lq.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        lq.eq(Dish::getStatus, 1);

        lq.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(lq);
        return R.success(list);
    }*/

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        LambdaQueryWrapper<Dish> lq = new LambdaQueryWrapper<>();
        lq.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        lq.eq(Dish::getStatus, 1);

        lq.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(lq);
        List<DishDto> dishDtoList = list.stream().map(item -> {
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(item, dto);
            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> lq1 = new LambdaQueryWrapper<>();
            lq1.eq(DishFlavor::getDishId, id);
            List<DishFlavor> flavors = dishFlavorService.list(lq1);
            dto.setFlavors(flavors);
            return dto;
        }).collect(Collectors.toList());
        return R.success(dishDtoList);
    }
}
